package com.ithema.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.FileInputStream;
import java.io.InputStream;

public class AliOssUtil {
    //
    private static final String ENDPOINT = "https://oss-cn-beijing.aliyuncs.com";

    private static final String ACCESS_KEY_ID = "LTAI5tHoUDCwsoXKapLGBGHf";
    // 阿里云账号 AccessKey Secret
    private static final String ACCESS_KEY_SECERT = "Rl9ZVq25YbFSwID8TKBDzwy5t4JFEO";
    // 存储空间名 称
    private static final String BUCKET_NAME = "big-enent123";

    public static String uploasFile(String objectName, InputStream in) {


        //创建ossClient实例
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECERT);
        String url = "";
        try {
            //填写字符串
            String content = "Hello OSS!";
            //创建putObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, in);
            PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
            url = String.format("https://%s.%s/%s", BUCKET_NAME, ENDPOINT.replace("https://", "").replace("http://", ""), objectName);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

}
